package service.impl;

import enum_var.AfterSaleType;
import utils.MyBatisUtils;
import dao.AfterSaleApplicationMapper;
import entity.AfterSaleApplication;
import enum_var.ApplicationStatus;
import service.AfterSaleApplicationService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 售后申请服务实现类
 */
public class AfterSaleApplicationServiceImpl implements AfterSaleApplicationService {

    @Override
    public void createApplication(AfterSaleApplication application) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            mapper.insert(application);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("创建售后申请失败: " + e.getMessage());
        }
    }

    @Override
    public AfterSaleApplication getApplicationById(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectById(id);
        }
    }

    @Override
    public List<AfterSaleApplication> getApplicationsByUserId(Long userId) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByUserId(userId);
        }
    }

    @Override
    public List<AfterSaleApplication> getApplicationsByStatus(ApplicationStatus status) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByStatus(status);
        }
    }


    @Override
    public List<AfterSaleApplication> getAllApplications() {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectAll();
        }
    }

    @Override
    public void updateApplication(AfterSaleApplication application) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            mapper.updateById(application);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("更新售后申请失败: " + e.getMessage());
        }
    }

    @Override
    public void deleteApplication(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            mapper.deleteById(id);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("删除售后申请失败: " + e.getMessage());
        }
    }

    @Override
    public List<AfterSaleApplication> getByUserIdAndStatus(Long userId, ApplicationStatus status) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByIdAndStatus(userId, status);
        }
    }

    @Override
    public List<AfterSaleApplication> getByUserIdAndType(Long userId, AfterSaleType type) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByIdAndType(userId, type);
        }
    }

    @Override
    public List<AfterSaleApplication> getByUserIdStatusAndType(Long userId, ApplicationStatus status, AfterSaleType type) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByIdStatusAndType(userId, status, type);
        }
    }

    @Override
    public List<AfterSaleApplication> getByStatusAndType(ApplicationStatus status, AfterSaleType type) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByStatusAndType(status, type);
        }
    }

    @Override
    public List<AfterSaleApplication> getByType(AfterSaleType type) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            AfterSaleApplicationMapper mapper = sqlSession.getMapper(AfterSaleApplicationMapper.class);
            return mapper.selectByType(type);
        }
    }
}
